﻿using System;
using System.Data;

namespace WeiSha.Core.Tree
{
	// Token: 0x02000031 RID: 49
	public class DataTableTree
	{
		// Token: 0x170000A4 RID: 164
		// (set) Token: 0x060001C1 RID: 449 RVA: 0x0000CE34 File Offset: 0x0000B034
		public DataTable DataSource
		{
			set
			{
				this._dataSource = value;
			}
		}

		// Token: 0x170000A5 RID: 165
		// (get) Token: 0x060001C2 RID: 450 RVA: 0x0000CE40 File Offset: 0x0000B040
		// (set) Token: 0x060001C3 RID: 451 RVA: 0x0000CE58 File Offset: 0x0000B058
		public string IdKeyName
		{
			get
			{
				return this._idKeyName;
			}
			set
			{
				this._idKeyName = value;
			}
		}

		// Token: 0x170000A6 RID: 166
		// (get) Token: 0x060001C4 RID: 452 RVA: 0x0000CE64 File Offset: 0x0000B064
		// (set) Token: 0x060001C5 RID: 453 RVA: 0x0000CE7C File Offset: 0x0000B07C
		public string ParentIdKeyName
		{
			get
			{
				return this._PatIdName;
			}
			set
			{
				this._PatIdName = value;
			}
		}

		// Token: 0x170000A7 RID: 167
		// (get) Token: 0x060001C6 RID: 454 RVA: 0x0000CE88 File Offset: 0x0000B088
		// (set) Token: 0x060001C7 RID: 455 RVA: 0x0000CEA0 File Offset: 0x0000B0A0
		public string TaxKeyName
		{
			get
			{
				return this._taxKeyName;
			}
			set
			{
				this._taxKeyName = value;
			}
		}

		// Token: 0x170000A8 RID: 168
		// (get) Token: 0x060001C8 RID: 456 RVA: 0x0000CEAC File Offset: 0x0000B0AC
		// (set) Token: 0x060001C9 RID: 457 RVA: 0x0000CEC4 File Offset: 0x0000B0C4
		public long Root
		{
			get
			{
				return this._root;
			}
			set
			{
				this._root = value;
			}
		}

		// Token: 0x060001CA RID: 458 RVA: 0x0000CED0 File Offset: 0x0000B0D0
		public DataTable BuilderTree(DataTable dt)
		{
			bool flag = dt == null;
			DataTable dataTable;
			if (flag)
			{
				dataTable = null;
			}
			else
			{
				bool flag2 = dt.Columns["Tree"] == null;
				if (flag2)
				{
					dt.Columns.Add(new DataColumn("Tree", typeof(string)));
				}
				bool flag3 = dt.Columns["isTop"] == null;
				if (flag3)
				{
					dt.Columns.Add(new DataColumn("isTop", typeof(string)));
				}
				bool flag4 = dt.Columns["isDown"] == null;
				if (flag4)
				{
					dt.Columns.Add(new DataColumn("isDown", typeof(string)));
				}
				DataTable dataTable2 = dt.Clone();
				this.sortFunc(dt, dataTable2, this.Root);
				bool flag5 = dataTable2.Rows.Count < dt.Rows.Count;
				if (flag5)
				{
					for (int i = 0; i < dt.Rows.Count; i++)
					{
						bool flag6 = false;
						for (int j = 0; j < dataTable2.Rows.Count; j++)
						{
							bool flag7 = dataTable2.Rows[j][this.IdKeyName].ToString() == dt.Rows[i][this.IdKeyName].ToString();
							if (flag7)
							{
								flag6 = true;
								break;
							}
						}
						bool flag8 = !flag6;
						if (flag8)
						{
							dataTable2.ImportRow(dt.Rows[i]);
						}
					}
				}
				dataTable = dataTable2;
			}
			return dataTable;
		}

		// Token: 0x060001CB RID: 459 RVA: 0x0000D08C File Offset: 0x0000B28C
		private void sortFunc(DataTable dt, DataTable sortDt, long parentId)
		{
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				string text = ((dt.Rows[i][this.ParentIdKeyName] == null) ? "" : dt.Rows[i][this.ParentIdKeyName].ToString());
				long num;
				long.TryParse(dt.Rows[i][this.ParentIdKeyName].ToString(), out num);
				bool flag = num == parentId;
				if (flag)
				{
					string text2 = this.line(dt, dt.Rows[i]);
					bool flag2 = this.isBottom(dt, dt.Rows[i], i);
					if (flag2)
					{
						text2 += "┗";
						dt.Rows[i]["isDown"] = true;
					}
					else
					{
						text2 += "┣";
						dt.Rows[i]["isDown"] = false;
					}
					bool flag3 = this.isTop(dt, dt.Rows[i], i);
					if (flag3)
					{
						dt.Rows[i]["isTop"] = true;
					}
					else
					{
						dt.Rows[i]["isTop"] = false;
					}
					dt.Rows[i]["Tree"] = text2;
					sortDt.ImportRow(dt.Rows[i]);
					bool flag4 = this.isChildren(dt, dt.Rows[i]);
					if (flag4)
					{
						long num2;
						long.TryParse(dt.Rows[i][this.IdKeyName].ToString(), out num2);
						this.sortFunc(dt, sortDt, num2);
					}
				}
			}
		}

		// Token: 0x060001CC RID: 460 RVA: 0x0000D288 File Offset: 0x0000B488
		private bool isChildren(DataTable dt, DataRow dr)
		{
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				bool flag = dt.Rows[i][this.ParentIdKeyName].ToString() == dr[this.IdKeyName].ToString();
				if (flag)
				{
					return true;
				}
			}
			return false;
		}

		// Token: 0x060001CD RID: 461 RVA: 0x0000D2F4 File Offset: 0x0000B4F4
		private bool isBottom(DataTable dt, DataRow dr, int index)
		{
			int num = ((this.TaxKeyName != "") ? Convert.ToInt32((dr[this.TaxKeyName] == null || dr[this.TaxKeyName].ToString() == "") ? 0 : dr[this.TaxKeyName]) : index);
			int num2 = num;
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				bool flag = dt.Rows[i][this.ParentIdKeyName].ToString() == dr[this.ParentIdKeyName].ToString();
				if (flag)
				{
					int num3 = ((this.TaxKeyName != "") ? Convert.ToInt32((dt.Rows[i][this.TaxKeyName] == null || dt.Rows[i][this.TaxKeyName].ToString() == "") ? 0 : dt.Rows[i][this.TaxKeyName]) : i);
					bool flag2 = num3 > num;
					if (flag2)
					{
						num2 = num3;
					}
				}
			}
			return num == num2;
		}

		// Token: 0x060001CE RID: 462 RVA: 0x0000D460 File Offset: 0x0000B660
		private bool isTop(DataTable dt, DataRow dr, int index)
		{
			int num = ((this.TaxKeyName != "") ? Convert.ToInt32((dr[this.TaxKeyName] == null || dr[this.TaxKeyName].ToString() == "") ? 0 : dr[this.TaxKeyName]) : index);
			int num2 = num;
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				bool flag = dt.Rows[i][this.ParentIdKeyName].ToString() == dr[this.ParentIdKeyName].ToString();
				if (flag)
				{
					int num3 = ((this.TaxKeyName != "") ? Convert.ToInt32((dt.Rows[i][this.TaxKeyName] == null || dt.Rows[i][this.TaxKeyName].ToString() == "") ? 0 : dt.Rows[i][this.TaxKeyName]) : i);
					bool flag2 = num3 < num;
					if (flag2)
					{
						num2 = num3;
					}
				}
			}
			return num == num2;
		}

		// Token: 0x060001CF RID: 463 RVA: 0x0000D5CC File Offset: 0x0000B7CC
		private string line(DataTable dt, DataRow dr)
		{
			string text = "";
			while (Convert.ToInt64((dr[this.ParentIdKeyName] == null || dr[this.ParentIdKeyName].ToString() == "") ? 0 : dr[this.ParentIdKeyName]) > this.Root)
			{
				for (int i = 0; i < dt.Rows.Count; i++)
				{
					bool flag = dt.Rows[i][this.IdKeyName].ToString() == dr[this.ParentIdKeyName].ToString();
					if (flag)
					{
						dr = dt.Rows[i];
						bool flag2 = this.isBottom(dt, dr, i);
						if (flag2)
						{
							text = "\u3000" + text;
						}
						else
						{
							text = "┃" + text;
						}
						break;
					}
				}
			}
			return text;
		}

		// Token: 0x04000082 RID: 130
		private DataTable _dataSource;

		// Token: 0x04000083 RID: 131
		private string _idKeyName;

		// Token: 0x04000084 RID: 132
		private string _PatIdName;

		// Token: 0x04000085 RID: 133
		private string _taxKeyName;

		// Token: 0x04000086 RID: 134
		private long _root;
	}
}
